<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="类型">
              <a-select v-model="parentId" placeholder="请选择" @select="typeClick">
                <a-select-option :value="item.value" v-for="(item,index) in optionType" :key="index" >{{ item.label }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="状态">
              <a-select v-model="enabled" placeholder="请选择" @select="enabledClick">
                <a-select-option :value="item.value" v-for="(item,index) in optionEnabled" :key="index" >{{ item.label }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-input-search placeholder="请输入类型关键字" @search="onSearch" >
              <a-button type="primary" slot="enterButton" icon="search">搜索</a-button>
            </a-input-search>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.createModal.add()">添加类型</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :showPagination="false"
    >
      <span slot="name" slot-scope="text">
        <ellipsis :length="15" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="iconUrl" slot-scope="text">
        <img :src="text" style="width:25px;height:25px">
      </span>
      <span slot="seqNum" slot-scope="text">
        <ellipsis :length="8" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="enabled" slot-scope="text">
        <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleChild(record)">子类型</a>
          <a-divider type="vertical" />
          <a @click="handleEdit(record)">编辑</a>
        </template>
      </span>
    </s-table>
    <add-model ref="createModal" @ok="handleOk" />
    <edit-model ref="modal" @ok="handleOk"/>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import AddModel from './modules/AddModel'
import EditModel from './modules/EditModel'
import { manageList } from '@/api/manage'
import utils from '@/utils/common'
import { mapActions } from 'vuex'
var timeStamp = Number(new Date())

const statusMap = {
  1: {
    status: 'success',
    text: '上线'
  },
  0: {
    status: 'default',
    text: '下线'
  }
}

export default {
  name: 'ManageList',
  components: {
    STable,
    Ellipsis,
    EditModel,
    AddModel
  },
  data () {
    return {
      parentId: '1',
      optionType: [
        { value: '1', label: '软件' },
        { value: '2', label: '游戏' }
      ],
      enabled: '',
      optionEnabled: [
        { value: '', label: '全部' },
        { value: 'true', label: '上线' },
        { value: 'false', label: '下线' }
      ],
      // 查询参数
      queryParam: {},
      keyword: '',
      startTime: '',
      endTime: '',
      pagination: {
        current: 1,
        pageSize: 20
      },
      // 表头
      columns: [
        {
          title: '类型',
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' }
        },
        {
          title: '图标',
          dataIndex: 'iconUrl',
          scopedSlots: { customRender: 'iconUrl' }
        },
        {
          title: '排序',
          dataIndex: 'seqNum',
          scopedSlots: { customRender: 'seqNum' }
        },
        {
          title: '状态',
          dataIndex: 'enabled',
          scopedSlots: { customRender: 'enabled' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: pagination => {
        return manageList(Object.assign(this.pagination, this.queryParam)).then(res => {
          return res.data
        })
      }
    }
  },
  filters: {
    statusFilter (type) {
      var val = 0
      if (type === true) {
        val = 1
      } else {
        val = 0
      }
      return statusMap[val].text
    },
    statusTypeFilter (type) {
      var val = 0
      if (type === true) {
        val = 1
      } else {
        val = 0
      }
      return statusMap[val].status
    }
  },
  created () {
    this.queryParam = {
      token: utils.AuthToken(timeStamp, this.$store.getters.userKey),
      timestamp: timeStamp,
      adminId: this.$store.getters.adminId,
      keyword: this.keyword,
      parentId: this.parentId,
      enabled: this.enabled
    }
    // this.loadData(this.queryParam)
  },
  methods: {
    ...mapActions(['IntoUpdate', 'intoList', 'GetTypeInfo']),
    enabledClick (val) { // 状态
      this.queryParam.enabled = val
      this.$refs.table.refresh()
    },
    typeClick (val) { // 类型
      this.queryParam.parentId = val
      this.$refs.table.refresh()
    },
    onSearch (val) { // 搜索
      this.queryParam.keyword = val
      this.$refs.table.refresh()
    },
    handleChild (record) { // 子类型
      this.$router.push('/manage/index/child/' + record.id)
    },
    handleEdit (record) { // 编辑
      this.GetTypeInfo({ id: record.id })
        .then((res) => {
          if (res.status === 200 || res.status === '200') {
            this.$refs.modal.edit(res.data)
          } else {
            this.requestFailed(res)
          }
        })
        .catch(err => this.requestFailed(err))
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    ApiUpdate (record, index) { // 发布 下架
      var data = {
        id: record.id,
        status: index
      }
      this.IntoUpdate(data)
        .then((res) => this.updateSuccess(res, record.id))
        .catch(err => this.requestFailed(err))
    },
    updateSuccess (res, key) {
      if (res.status === 200 || res.status === '200') {
        this.$notification['success']({
          message: '成功',
          description: res.message,
          duration: 4
        })
        this.$refs.table.refresh()
      } else {
        this.requestFailed(res)
      }
    },
    requestFailed (err) {
      this.$notification['error']({
        message: '错误',
        description: err.message || '请求出现错误，请稍后再试',
        duration: 4
      })
    }
  }
}
</script>
